<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/29 0029
 * Time: 下午 5:37
 */

/**模板显示截取字符串
 * @param $text 字符串
 * @param $length 多少字符后隐藏
 * @return string
 */
function subtext($text, $length)
{
    if(mb_strlen($text, 'utf8') > $length)
        return mb_substr($text, 0, $length, 'utf8').'...';
    return $text;
}

/**
 * 递归输出
 * @title  后台左侧列表栏目显示
 * @param $items 数组
 * @param string $id
 * @param string $pid 父栏目id
 * @param string $son 子栏目数组
 * @return array
 */
function getMenu($items, $id = 'id', $pid = 'pid', $son = 'children')
{
    $tree = array();
    $tmpMap = array();

    foreach ($items as $item) {
        $tmpMap[$item[$id]] = $item;
    }
    foreach ($items as $item) {
        if (isset($tmpMap[$item[$pid]])) {
            $tmpMap[$item[$pid]][$son][] = &$tmpMap[$item[$id]];
        } else {
            $tree[] = &$tmpMap[$item[$id]];
        }
    }
    return $tree;
}

/**
 * 检测用户权限
 * @param array $menus 权限列表
 * @param string $url 检测的权限
 * @return boolean
 */
function has_menu ($menus,$url = null){
    $nowUrl = $url ? $url : ucfirst(\think\Request::instance()->controller()).'/'. strtolower(\think\Request::instance()->action());
    $allowList = array('index/index','index/profile','index/upload','index/trash','index/ueditor');
    if(in_array($nowUrl,$allowList)) return true;
    $result = false;
    foreach($menus as $k=>$v){
        if($v['url'] == $nowUrl){
            $result = true;
            break;
        }else{
            if(isset($v['children'])) {
                foreach ($v['children'] as $kk => $vv) {
                    if ($vv['url'] == $nowUrl) {
                        $result = true;
                        break;
                    }else{
                        if(isset($vv['children'])) {
                            foreach ($vv['children'] as $kkk => $vvv) {
                                if ($vvv['url'] == $nowUrl) {
                                    $result = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return $result;
}

/**
 * 全局变量
 *
 * @param $name 变量名
 * @param string $value 变量值
 * @return mixed 返回值
 */
if (!function_exists('v')) {
    function v($name = null, $value = '[null]')
    {
        static $vars = [];
        if (is_null($name)) {
            return $vars;
        } else if ($value == '[null]') {
            //取变量
            $tmp = $vars;
            foreach (explode('.', $name) as $d) {
                if (isset($tmp[$d])) {
                    $tmp = $tmp[$d];
                } else {
                    return null;
                }
            }
            return $tmp;
        } else {
            //设置
            $tmp = &$vars;
            foreach (explode('.', $name) as $d) {
                if (!isset($tmp[$d])) {
                    $tmp[$d] = [];
                }
                $tmp = &$tmp[$d];
            }
            return $tmp = $value;
        }
    }
}